package com.dinggao.crossgo.module.roadbook.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dinggao.crossgo.R;
import com.dinggao.crossgo.core.base.BaseActivity;
import com.dinggao.crossgo.core.constant.CommonConstant;
import com.dinggao.crossgo.core.manage.ApiManager;
import com.dinggao.crossgo.core.paser.FastJsonUtils;
import com.dinggao.crossgo.core.util.LogUtils;
import com.dinggao.crossgo.core.util.SharedPreferencesUtil;
import com.dinggao.crossgo.module.roadbook.adapter.RoadBookMineAdapter;
import com.dinggao.crossgo.module.roadbook.data.RoadBookMineBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RoadBookMineActivity extends BaseActivity {

    public static final String TAG = "RoadBookMineActivity";

    @BindView(R.id.rood_book_mine_recycler)
    RecyclerView roodBookMineRecycler;
    @BindView(R.id.mine_delete_back)
    ImageView mineDeleteBack;

    @BindView(R.id.mine_bottom_layout)
    RelativeLayout mineBottomLayout;
    @BindView(R.id.mine_delete_icon)
    TextView mineDeleteIcon;
    @BindView(R.id.image_empty)
    ImageView imageEmpty;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.roadbook_size)
    TextView roadbookSize;

    private RoadBookMineAdapter adapter;
    public static int show = 0;
    private List<RoadBookMineBean.DataBean.ItemsBean> roadBookMineBeanList = new ArrayList<>();
    private List<Integer> removeList = new ArrayList<>();
    private boolean chooseAll = false, cancle = false;

    private int mNextRequestPage = 1;
    private static final int PAGE_SIZE = 10;
    private  String accessToken;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_road_book_mine;
    }

    @Override
    protected void onInitialization(Bundle bundle) {

        initView();
        initData(mNextRequestPage);
    }

    private void initView() {
        adapter = new RoadBookMineAdapter(R.layout.item_rood_book_mine_recycler, roadBookMineBeanList);
        roodBookMineRecycler.setLayoutManager(new LinearLayoutManager(this));
        roodBookMineRecycler.setAdapter(adapter);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_empty, null);
        adapter.setEmptyView(view);
       // adapter.setUpFetchEnable(true);

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {

                CheckBox checkBox = ((CheckBox) view.findViewById(R.id.checkbox_mine));
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            LogUtils.e("tag", position + "");
                            removeList.add(position);
                            LogUtils.e("tag", removeList.get(position) + "");
                        } else {
                            if (removeList != null && removeList.size() > 0) {
                                removeList.remove(position);
                            }

                        }
                    }
                });
            }
        });


        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (!cancle) {
                    Intent intent=new Intent(RoadBookMineActivity.this, RoadBookParentDetailsActivity.class);
                    intent.putExtra("id",roadBookMineBeanList.get(position).getId());
                    startActivity(intent);
                }
            }
        });

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }


    private void loadMore() {
        mNextRequestPage++;
        initData(mNextRequestPage);
    }


    private void refresh(){
        mNextRequestPage=1;
        adapter.setEnableLoadMore(false);
        initData(mNextRequestPage);
    }



    private void initData(int mNextRequestPage) {


        accessToken = SharedPreferencesUtil.getSharedPreferencesString(RoadBookMineActivity.this, CommonConstant.USERINFO_SHREDPREFERENCE,
                CommonConstant.USRERINFO_SHRAREDPREFERCE_ACCESS_TOKEN);
        LogUtils.e(TAG, accessToken);

        OkGo.<String>get(ApiManager.ROADBOOK_MYBOOK)

                .headers("accessToken",accessToken)
                .tag(this)
                .params("limit", PAGE_SIZE)
                .params("page", mNextRequestPage)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {


                        if (response.body() != null ) {
                            LogUtils.e(TAG, response.body());
                            imageEmpty.setVisibility(View.GONE);
                            RoadBookMineBean roadBookMineBean = FastJsonUtils.getObject(response.body(), RoadBookMineBean.class);
                            roadBookMineBeanList.addAll(roadBookMineBean.getData().getItems());
                            int size = roadBookMineBean.getData().getItems().size();
                            if (size<PAGE_SIZE){
                                //第一页如果不够一页就不显示没有更多数据布局
                                adapter.loadMoreEnd(false);
                                Toast.makeText(RoadBookMineActivity.this, "no more data", Toast.LENGTH_SHORT).show();
                            }else {
                                adapter.loadMoreComplete();
                            }
                            roadbookSize.setText(size+"本路书");
                            adapter.notifyDataSetChanged();

                            mineDeleteBack.setClickable(true);
                        } else {
                            imageEmpty.setVisibility(View.VISIBLE);
                            mineDeleteBack.setClickable(false);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LogUtils.e(TAG, response.getException().toString());

                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.mine_delete_back, R.id.mine_delete_icon, R.id.mine_publish_text, R.id.mine_choose_all, R.id.mine_choose_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mine_delete_back:
                finish();
                break;
            case R.id.mine_delete_icon:


                if (!cancle) {
                    cancle = true;
                    show = 1;
                    adapter.notifyDataSetChanged();
                    mineDeleteBack.setVisibility(View.GONE);
                    mineDeleteIcon.setBackground(null);
                    mineDeleteIcon.setText("取消");
                    mineBottomLayout.setVisibility(View.VISIBLE);
                } else {
                    show = 0;
                    cancle = false;
                    adapter.notifyDataSetChanged();
                    mineDeleteIcon.setBackground(getResources().getDrawable(R.drawable.mine_delete_icon));
                    mineDeleteIcon.setText("");
                    mineDeleteBack.setVisibility(View.VISIBLE);
                    mineBottomLayout.setVisibility(View.GONE);

                }


                break;
            case R.id.mine_publish_text:
                startActivity(new Intent(RoadBookMineActivity.this, RoadBookParentDetailsActivity.class));
                break;
            case R.id.mine_choose_all:
                show = 2;
                chooseAll = true;
                adapter.notifyDataSetChanged();
                break;
            case R.id.mine_choose_cancle:
                //删除
                if (chooseAll) {
                    roadBookMineBeanList.clear();
                } else {
                    for (int i = 0; i < removeList.size(); i++) {
                        roadBookMineBeanList.remove(i);
                    }
                }
                chooseAll = false;
                show = 0;
                adapter.notifyDataSetChanged();

                mineDeleteIcon.setBackground(getResources().getDrawable(R.drawable.mine_delete_icon));
                mineDeleteIcon.setText("");
                mineDeleteBack.setVisibility(View.VISIBLE);
                mineBottomLayout.setVisibility(View.GONE);

                OkGo.<String>delete("")
                        .tag(this)
                        .headers("accessToken",accessToken)
                        .params("roadBookId","")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {

                            }
                        });
                break;

        }
    }


}
